package com.example.statistics.net.callback

import android.app.Activity
import com.jhj.httplibrary.HttpCall
import com.jhj.httplibrary.callback.DownloadHttpCallback
import com.jhj.prompt.fragment.PercentFragment
import com.jhj.prompt.fragment.base.OnDialogShowOnBackListener
import java.io.File

abstract class DownloadDialogHttpCallback(activity: Activity, val downloadFile: File, val msg: String = "正在下载中...") :
        DownloadHttpCallback(activity, downloadFile) {

    lateinit var dialog: PercentFragment.Builder

    override fun onStart() {
        super.onStart()
        dialog = PercentFragment.Builder(mActivity)
                .setDialogShowOnBackListener(object : OnDialogShowOnBackListener<PercentFragment> {
                    override fun cancel(fragment: PercentFragment) {
                        mActivity.finish()
                        HttpCall.pauseDownload(mActivity)
                    }
                })
                .setText(msg)
                .setCancelOnTouchOut(false)
                .show()
    }


    override fun onProgress(percent: Int) {
        mActivity.runOnUiThread {
            dialog.setProgress(percent)
            if (percent == dialog.getMaxProgress()) {
                dialog.dismiss()
            }
        }
    }

    override fun onFinish() {
        super.onFinish()
        if (dialog.isShow()) {
            dialog.dismiss()
        }
    }
}